package yxy.game.pm2.command.godwar;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.godwar;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.GodWarMessage;
import yxy.game.pm2.module.arena.RankArena;
import yxy.game.pm2.module.godwar.GodWarHandler;

/** 副本战神殿 战神殿挑战战斗开始 */
@MessageType(proto = Command.GodWarChanllengeExecute, classtype = GodWarMessage.GodWarChanllengeExecuteRequest.class)
public class CMD_GodWarChanllengeExecute extends Command<GodWarMessage.GodWarChanllengeExecuteRequest> {

	@Override
	public void execute(MessageSource messageSource, GodWarMessage.GodWarChanllengeExecuteRequest request) {
		String playerCode = messageSource.getTag();
		int dataId = request.getId();
		int evolve = request.getEvolve();

		GodWarMessage.GodWarChanllengeExecuteResponse.Builder response = GodWarMessage.GodWarChanllengeExecuteResponse.newBuilder();

		// 判断参数
		switch (evolve) {
		case 1:
		case 5:
		case 10:
			break;
		default:
			send(messageSource, response, Result.ParamError);
			return;
		}
		// 判断资格
		int rank = RankArena.getRank(playerCode).rank(playerCode);
		godwar data = DataCenter.getData(dataId, godwar.class);
		if (data == null || rank <= 0 || rank > data.getCondition()) {
			send(messageSource, response, Result.GodWarArenaRankNotFit);// 你的竞技场排名不具备挑战资格
			return;
		}

		try {
			Result result = new GodWarHandler().battleResponseExecute(playerCode, data, evolve, response);
			send(messageSource, response, result);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, GodWarMessage.GodWarChanllengeExecuteResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}